const Router = require("koa-router");
const fs = require("fs");
const path = require("path");
const router = new Router({ prefix: "/smscode" });

router.get("/getcurrentcode/:random", (ctx, next) => {
  //1.读取配置文件的内容
  const codePath = path.resolve(__dirname, "../data/code");
  let targetContent = fs.readFileSync(codePath, { encoding: "utf8" });
  ctx.response.body = targetContent;
});
router.get("/savecurrentcode/:code/:random", (ctx, next) => {
  const codePath = path.resolve(__dirname, "../data/code");
  let result = fs.writeFileSync(codePath, ctx.request.params.code);
  setTimeout(() => {
    fs.writeFileSync(codePath, "");
  }, 2 * 60 * 1000);
  console.log(result);
  ctx.response.body = "ok";
});
router.get("/clearcurrentcode/:random", (ctx, next) => {
  const codePath = path.resolve(__dirname, "../data/code");
  fs.writeFileSync(codePath, "");
});
router.get("/savelog/:data", (ctx, next) => {
  const codePath = path.resolve(__dirname, "../data/log");
  fs.appendFileSync(codePath, "\n" + ctx.request.params.data);
  ctx.response.body = "ok";
});
router.get("/getlog/:random", (ctx, next) => {
  //1.读取配置文件的内容
  const codePath = path.resolve(__dirname, "../data/log");
  let targetContent = fs.readFileSync(codePath, { encoding: "utf8" });
  ctx.response.body = targetContent;
});
module.exports = router;
